package cn.ainit.yz.sprk;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ainit.sys.common.dao.BusinessBaseDao;
import cn.ainit.sys.utils.UUID32;
import cn.ainit.yz.sprk.entity.Sprk;
import cn.ainit.yz.sprk.entity.SprkItem;


@Transactional
@Repository
public class SprkDaoImpl extends BusinessBaseDao implements SprkDao{
	@Override
	public boolean saveSprk(Sprk sprk, List<SprkItem> sprkItems)
			throws Exception {
		this.getSessionFactory().getCurrentSession().save(sprk);
		for (int i = 0; i < sprkItems.size(); i++) {
			sprkItems.get(i).setSpItemId(UUID32.newGuid());
			sprkItems.get(i).setSpId(sprk.getSpId());
			this.getSessionFactory().getCurrentSession().save(sprkItems.get(i));
		}
		return true;
	}
	
	@Override
	public boolean updateSprk(Sprk sprk, List<SprkItem> sprkItems)
			throws Exception {
		List<SprkItem> oldItems = new ArrayList<SprkItem>();
		List<SprkItem> newItems = new ArrayList<SprkItem>();
		//更新操作
		//保存
		//缓存旧的明细IDS
		StringBuffer spItemIdS = new StringBuffer("");
		//遍历明细   判断条件
		for (int i = 0; i < sprkItems.size(); i++) {
			//如果有主键，则是原有记录   拼接原有记录id 否则为新记录
			System.out.println(sprkItems.get(i).getSpItemId()+"----00000000");
			if (sprkItems.get(i).getSpItemId() != null && !sprkItems.get(i).getSpItemId().equals("")) {
				oldItems.add(sprkItems.get(i));
				spItemIdS.append("'"+sprkItems.get(i).getSpItemId()+"',");
			}else {
				newItems.add(sprkItems.get(i));
			}
		}
		String ids = spItemIdS.toString();
		if (!ids.equals("")) {
			ids = ids.substring(0, ids.length()-1);
		}
		//先删除不存在的
		if (!ids.trim().equals("")) {
			String sql = " delete from yz_sprk_item spi where spi.sp_id = '"+sprk.getSpId()+"' ";
			sql += " and spi.sp_item_id not in ("+ids+") ";
//			System.out.println(sql);
			this.getJdbcTemplate().execute(sql);
		}
		//更新主表
		this.getSessionFactory().getCurrentSession().update(sprk);
		//更新明细
		for (int i = 0; i < oldItems.size(); i++) {
			this.getSessionFactory().getCurrentSession().update(oldItems.get(i));
		}
		//更新主表
		for (int i = 0; i < newItems.size(); i++) {
			newItems.get(i).setSpItemId(UUID32.newGuid());
			newItems.get(i).setSpId(sprk.getSpId());
			this.getSessionFactory().getCurrentSession().save(newItems.get(i));
		}
		return true;
	}
	@Override
	public String sprkToDaohuo(String sprkId, String oId) throws Exception {
		String putValue="";
		Connection conn = null;
		CallableStatement cs =null;
		try {
			 conn= this.getJdbcTemplate().getDataSource().getConnection();
			 String sql = "{Call PK_YZ.up_RkToJH(?,?,?)}";
			 cs = conn.prepareCall(sql);
			 cs.setString(1,sprkId);
			 cs.setString(2,oId);
			 cs.registerOutParameter(3,oracle.jdbc.OracleTypes.VARCHAR);
			 cs.execute();
			 putValue = cs.getString(3);
		} catch (Exception e) {
			e.getMessage();
			throw e;
		}finally{
			if(cs != null){
				cs.close();
			}
			if(conn != null){
				conn.close();
			}
		}
		return putValue;
	}
	@Override
	public String getHId(String xtId) throws Exception {
		String hId = "";
		try {
			String sql = "select b.h_id from bj_isbn_cip b WHERE b.xt_id = '"+xtId+"' and is_destroy = '0'";
			hId = this.jdbcTemplate.queryForObject(sql,String.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hId;
	}
}
